package edu.washington.ling.syntric.consistency;

import edu.washington.ling.syntric.struct.tree.Token;
import edu.washington.ling.syntric.struct.tree.Tree;

public class SmallClauseRule extends ConsistencyRule { 
	
	@Override
	public void apply(Tree ds) {
		Token dsRoot = ds.getDSRoot();
		apply(dsRoot, ds);
	}

	public void apply(Token t, Tree ds) {
		for (Token child: t.getDSDependents()) {
			apply(child, ds);
		}
		Token k2Child = null;
		Token k2sChild = null;
		for (Token child: t.getDSDependents()) {
			if (child.getDSRelation().equals("k2")) {
				k2Child = child;
			}
			if (child.getDSRelation().equals("k2s")) {
				k2sChild = child;
			}
		}
		for (Token child: t.getECDependents()) {
			if (child.getDSRelation().equals("k2")) {
				k2Child = child;
			}
		}
		
		// Diagnostic test
		if (k2Child != null && k2sChild != null) {
			
			// Operation					
			
			// Get index for new EC
			int index = ds.nextCoindex();
			
			// Create a CASE ec and attach
			// to k2s child with sc-arg relation
			Token ec = ConsistencyUtils.createMovementEC("CASE", "sc-arg", index);
	
			// Add ec to k2s child
			k2sChild.addECDependent(ec);
					
			// Set target index of k2 child
			k2Child.setDSTargetIndex(index);
		}
	}
}	
